export class EventEmitter {
  constructor() {
    this.handlers = {};
  }

  /**
   * @param {string} key
   * @param {(...args: any[]) => void} handler
   */
  on(key, handler) {
    if (!this.handlers[key]) {
      this.handlers[key] = [handler];
      return;
    }
    this.handlers[key].push(handler);
  }

  emit(key, ...args) {
    if (this.handlers[key]) {
      this.handlers[key].forEach((h) => h(...args));
    }
  }
}
